﻿Imports TwitterVB2
Imports System.IO.File
Public Class frmSpamBlocker

    Dim tw As New TwitterAPI
    Private IsFormBeingDragged As Boolean = False
    Private MousePos As New System.Drawing.Point(0, 0)

    Private Sub frmSpamBlocker_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Try
            Dim thePath As String = Application.StartupPath & "\" & frmLogin.txtUsername.Text.ToLower & "_spamblocker.dat"
            If lstBlacklist.Items.Count > 0 Then
                Dim myWriter As New IO.StreamWriter(thePath)

                For Each myItem As String In lstBlacklist.Items
                    With myItem
                        myWriter.WriteLine(myItem)

                        ' & "|_SP_|" & myItem.SubItems(1).Text
                        '& "|" & myItem.SubItems(2).Text
                    End With
                Next
                myWriter.Close()
                Me.Visible = False


            End If
        Catch ex As Exception

        End Try


    End Sub

    Private Sub frmSpamBlocker_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If e.CloseReason = CloseReason.UserClosing Then
            'Don't close permanently so we don't lose any information we need until the end.
            e.Cancel = True
            'Me.Visible = False

            'ShowInTaskbar = False
            Me.WindowState = FormWindowState.Minimized

        End If

    End Sub

    Private Sub frmSpamBlocker_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        ToolTip1.SetToolTip(Label2, "Add phrases or URLs to automatically report spam AND block the user.  Press enter after you're done to add it to the list.")

        chkBlock.Checked = My.Settings.s_chkBlock
        chkReport.Checked = My.Settings.s_chkReport
        chkDeleteSpam.Checked = My.Settings.s_chkDeleteSpam

        lblMin.Parent = PictureBox2
        lblMax.Parent = PictureBox2
        lblExit.Parent = PictureBox2

        lblMin.BackColor = Color.Transparent
        lblMax.BackColor = Color.Transparent
        lblExit.BackColor = Color.Transparent

    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If e.KeyCode = Keys.Enter Then
            If (Len(TextBox1.Text) > 0) Then


                lstBlacklist.Items.Add(TextBox1.Text, True)
                lstBlacklist.SelectedIndex = lstBlacklist.Items.Count - 1
                TextBox1.Text = ""

            End If
        End If

    End Sub

    Private Sub chkSpamBlock_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkSpamBlock.CheckedChanged

        If chkSpamBlock.Checked Then

            tmrSpamCheck.Enabled = True
            MsgBox("Smart Promoter will now check for spam and report + block if it finds anything in your mentions or DMs every 30 minutes.")

        Else

            tmrSpamCheck.Enabled = False

        End If

    End Sub

    Private Sub tmrSpamCheck_Tick(sender As System.Object, e As System.EventArgs) Handles tmrSpamCheck.Tick

        Dim i As Integer

        tw.AuthenticateWith(frmMain.consumer, frmMain.consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

        Button1.Text = "Searching..."

        'lstSpammers.Items.Add(twMention.SenderScreenName + ": " + twMention.Text)
        For Each twDM As TwitterDirectMessage In tw.DirectMessages

            Dim strUser = twDM.SenderScreenName
            Dim intID = twDM.SenderID

            'lstSpammers.Items.Add(strUser + ": " + twDM.Text.ToString.ToLower)

            For i = 0 To lstBlacklist.Items.Count - 1

                Dim badPhrase = lstBlacklist.Items.Item(i)

                If InStr(twDM.Text.ToString.ToLower, badPhrase.ToString.ToLower) Then

                    lstSpammers.BeginUpdate()

                    Dim n As New ListViewItem

                    n = lstSpammers.Items.Add(DateAndTime.Now)
                    n.SubItems.Add(strUser)
                    n.SubItems.Add(badPhrase)

                    lstSpammers.Refresh()
                    lstSpammers.EndUpdate()

                    'lstSpammers.Items.Add( & " - User: " & strUser & " Reason: " & badPhrase)

                    If chkReport.Checked = True Then

                        tw.ReportSpam(intID)

                    End If

                    If chkBlock.Checked = True Then

                        tw.BlockUser(intID)

                    End If

                    If chkDeleteSpam.Checked = True Then

                        tw.DeleteDirectMessage(twDM.ID)

                    End If

                End If

            Next

        Next

        For Each twMention As TwitterStatus In tw.Mentions

            Dim strUser = twMention.User.ScreenName
            Dim intID = twMention.User.ID

            For i = 0 To lstBlacklist.Items.Count - 1

                Dim badPhrase = lstBlacklist.Items.Item(i)

                If InStr(twMention.Text.ToString.ToLower, badPhrase.ToString.ToLower) Then

                    lstSpammers.BeginUpdate()

                    Dim n As New ListViewItem

                    n = lstSpammers.Items.Add(DateAndTime.Now)
                    n.SubItems.Add(strUser)
                    n.SubItems.Add(badPhrase)

                    lstSpammers.Refresh()
                    lstSpammers.EndUpdate()

                    If chkReport.Checked = True Then

                        tw.ReportSpam(intID)

                    End If

                    If chkBlock.Checked = True Then

                        tw.BlockUser(intID)

                    End If

                End If

            Next

        Next

        Button1.Text = "Done!"
        lblStatus.Text = "Updated on: " & DateAndTime.Now
        tmrResetButton.Enabled = True

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        'On Error Resume Next

        Dim i As Integer

        tw.AuthenticateWith(frmMain.consumer, frmMain.consumersecret, My.Settings.m_strToken, My.Settings.m_strTokenSecret)

        Button1.Text = "Searching..."

        'lstSpammers.Items.Add(twMention.SenderScreenName + ": " + twMention.Text)
        For Each twDM As TwitterDirectMessage In tw.DirectMessages

            Dim strUser = twDM.SenderScreenName
            Dim intID = twDM.SenderID

            'lstSpammers.Items.Add(strUser + ": " + twDM.Text.ToString.ToLower)

            For i = 0 To lstBlacklist.Items.Count - 1

                Dim badPhrase = lstBlacklist.Items.Item(i)

                If InStr(twDM.Text.ToString.ToLower, badPhrase.ToString.ToLower) Then

                    lstSpammers.BeginUpdate()

                    Dim n As New ListViewItem
                    n = lstSpammers.Items.Add(DateAndTime.Now)
                    n.SubItems.Add(strUser)
                    n.SubItems.Add(badPhrase)

                    lstSpammers.Refresh()
                    lstSpammers.EndUpdate()

                    'lstSpammers.Items.Add(DateAndTime.Now & " - User: " & strUser & " Reason: " & badPhrase)

                    If chkReport.Checked = True Then

                        tw.ReportSpam(intID)

                    End If

                    If chkBlock.Checked = True Then

                        tw.BlockUser(intID)

                    End If

                    If chkDeleteSpam.Checked = True Then

                        tw.DeleteDirectMessage(twDM.ID)

                    End If

                End If

            Next

        Next


        For Each twMention As TwitterStatus In tw.Mentions

            Dim strUser = twMention.User.ScreenName
            Dim intID = twMention.User.ID

            For i = 0 To lstBlacklist.Items.Count - 1

                Dim badPhrase = lstBlacklist.Items.Item(i)

                If InStr(twMention.Text.ToString.ToLower, badPhrase.ToString.ToLower) Then

                    lstSpammers.BeginUpdate()

                    Dim n As New ListViewItem

                    n = lstSpammers.Items.Add(DateAndTime.Now)
                    n.SubItems.Add(strUser)
                    n.SubItems.Add(badPhrase)

                    lstSpammers.Refresh()
                    lstSpammers.EndUpdate()

                    If chkReport.Checked = True Then

                        tw.ReportSpam(intID)

                    End If

                    If chkBlock.Checked = True Then

                        tw.BlockUser(intID)

                    End If

                End If

            Next

        Next


        Button1.Text = "Done!"
        tmrResetButton.Enabled = True

    End Sub

    Private Sub lstBlacklist_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles lstBlacklist.MouseDoubleClick

        If MsgBox("Delete this tweet?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then

            lstBlacklist.Items.RemoveAt(lstBlacklist.SelectedIndex)

        End If

    End Sub

    Private Sub tmrResetButton_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrResetButton.Tick

        On Error Resume Next

        Button1.Text = "Search Again"
        tmrResetButton.Enabled = False

    End Sub

    Private Sub chkDeleteSpam_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkDeleteSpam.CheckedChanged

        My.Settings.s_chkDeleteSpam = chkDeleteSpam.Checked
        My.Settings.Save()

    End Sub

    Private Sub chkBlock_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkBlock.CheckedChanged

        My.Settings.s_chkBlock = chkBlock.Checked
        My.Settings.Save()

    End Sub

    Private Sub chkReport_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkReport.CheckedChanged

        My.Settings.s_chkReport = chkReport.Checked
        My.Settings.Save()

    End Sub

    Private Sub PictureBox2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown

        If e.Button = MouseButtons.Left Then

            IsFormBeingDragged = True

            MousePos = e.Location

        End If


    End Sub

    Private Sub PictureBox2_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove

        If IsFormBeingDragged Then

            Dim temp As Point = New Point(Me.Location + (e.Location - MousePos))
            Me.Location = temp
            temp = Nothing

        End If

    End Sub

    Private Sub PictureBox2_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp

        If e.Button = MouseButtons.Left Then IsFormBeingDragged = False

    End Sub

    Private Sub lblMin_Click(sender As System.Object, e As System.EventArgs) Handles lblMin.Click

        On Error Resume Next

        'If Me.WindowState = FormWindowState.Minimized Then

        Me.Visible = False

        'End If

    End Sub

    Private Sub lblExit_Click(sender As System.Object, e As System.EventArgs) Handles lblExit.Click

        Me.Visible = False

    End Sub

    Private Sub ClearToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ClearToolStripMenuItem.Click

        If MsgBox("Clear the status box?", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then

            lstSpammers.Clear()

        End If

    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class